package com.jinmdz.fmis.api.api.service;

import com.jinmdz.fmis.api.api.model.funeral.*;
import com.jinmdz.fmis.api.base.BaseService;
import com.jinmdz.fmis.api.constant.MessageConst;
import com.jinmdz.fmis.api.model.config.dictionary.BusinessLogCode;
import com.jinmdz.fmis.api.model.config.dictionary.DictionaryCode;
import com.jinmdz.fmis.api.model.config.dictionary.OperationLogCode;
import com.jinmdz.fmis.api.model.system.UserItem;
import com.jinmdz.fmis.api.wrapper.DictionaryWrapper;
import com.jinmdz.fmis.api.wrapper.OperationWrapper;
import com.jinmdz.fmis.common.ext.StringBuilderExt;
import com.jinmdz.fmis.common.util.BeanUtil;
import com.jinmdz.fmis.common.util.DataUtil;
import com.jinmdz.fmis.common.util.IdCardUtil;
import com.jinmdz.fmis.common.util.third.Pinyin4jUtil;
import com.jinmdz.fmis.core.base.BaseResult;
import com.jinmdz.fmis.core.constant.EDictCode;
import com.jinmdz.fmis.core.exception.ActionException;
import com.jinmdz.fmis.dao.dao.FamilyAgentDao;
import com.jinmdz.fmis.dao.dao.FamilyMemberDao;
import com.jinmdz.fmis.dao.dao.FuneralBusinessDao;
import com.jinmdz.fmis.dao.dao.FuneralDeadDao;
import com.jinmdz.fmis.dao.model.funeral.FamilyMemberItem;
import com.jinmdz.fmis.dao.model.funeral.FuneralBusinessViewItem;
import com.jinmdz.fmis.mapper.entity.*;
import com.jinmdz.fmis.mapper.mapper.FamilyAgentMapper;
import com.jinmdz.fmis.mapper.mapper.FamilyMemberMapper;
import com.jinmdz.fmis.mapper.mapper.FuneralBusinessMapper;
import com.jinmdz.fmis.mapper.mapper.FuneralDeadMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * CremationArchivesService类
 *
 * @author LiCongLu
 * @date 2020-01-19 13:39
 */
@Service("cremationArchivesService")
public class CremationArchivesService extends BaseService {

    @Resource
    private FuneralBusinessService funeralBusinessService;

    @Resource
    private FuneralBusinessMapper funeralBusinessMapper;

    @Resource
    private FuneralDeadMapper funeralDeadMapper;
    @Resource
    private DictionaryWrapper dictionaryWrapper;

    @Resource
    private FamilyMemberMapper familyMemberMapper;

    @Resource
    private FamilyAgentMapper familyAgentMapper;

    @Resource
    private FuneralBusinessDao funeralBusinessDao;

    @Resource
    private FuneralDeadDao funeralDeadDao;

    @Resource
    private FamilyMemberDao familyMemberDao;

    @Resource
    private FamilyAgentDao familyAgentDao;

    @Resource
    private OperationWrapper operationWrapper;

    @Resource
    private OperationLogCode operationLogCode;

    @Resource
    private BusinessLogCode businessLogCode;

    @Resource
    private DictionaryCode dictionaryCode;


    /**
     * 根据funeral_dead表中businessCode加载火化逝者信息
     *
     * @param data 业务编码数据
     * @return
     * @author LiCongLu
     * @date 2020-01-19 13:45
     */
    public BaseResult<CremationArchivesItem> loadCremationArchivesWithBusinessCode(BusinessCodeData data) {
        String businessCode = data.getBusinessCode();
        // 通过业务编码获取殡葬信息
        FuneralBusinessViewItem funeralItem = funeralBusinessDao.getFuneralBusinessViewByBusinessCode(businessCode);
        if (DataUtil.isNull(funeralItem) || DataUtil.isNull(funeralItem.getId(), funeralItem.getVersion())) {
            throw exception(MessageConst.NO_FUNERAL_CODE);
        }

        // 创建响应实体
        CremationArchivesItem loadItem = new CremationArchivesItem();

        // 获取逝者信息
        loadItem.setFuneralDead(funeralDeadDao.getFuneralDeadByBusinessCode(businessCode));
        //更新逝者家属字典
        // 设置字典文本
        dictionaryWrapper.resetDataText(loadItem);
        // 获取家属信息
        loadItem.setFamilyMember(familyMemberDao.getFamilyMemberByBusinessCode(businessCode));
        // 获取承办人信息
        loadItem.setFamilyAgent(familyAgentDao.getFamilyAgentByBusinessCode(businessCode));

        // 返回响应结果
        return successData(loadItem);
    }

    /**
     * 提交并保存火化逝者信息登记界面上的表单数据
     *
     * @param userItem 当前帐号
     * @param data     逝者档案数据
     * @return
     * @author LiCongLu
     * @date 2020-01-19 13:51
     */
    @Transactional(rollbackFor = Exception.class)
    public BaseResult<CremationArchivesItem> saveCremationArchives(UserItem userItem, CremationArchivesData data) throws ActionException {
        // 验证请求数据为空
        if (DataUtil.isNull(data.getFuneralDead(), data.getFamilyMember())) {
            return failure("逝者及家属不能为空");
        }

        String businessCode = data.getFuneralDead().getBusinessCode();
        // 业务编码不为空
        if (DataUtil.invalid(businessCode)) {
            // 不存在业务编码时，认定为新增，生成新业务编码
            businessCode = funeralBusinessService.getBusinessCodeAsNew();
            insertCremationArchives(userItem, businessCode, data);
        } else {
            updateCremationArchives(userItem, data);
        }
        return loadCremationArchivesWithBusinessCode(new BusinessCodeData(businessCode));
    }

    /**
     * 插入殡葬信息
     *
     * @param userItem     当前帐号
     * @param businessCode 业务编码
     * @param data         逝者档案数据
     * @return
     * @author LiCongLu
     * @date 2020-02-06 16:55
     */
    private void insertCremationArchives(UserItem userItem, String businessCode, CremationArchivesData data) throws ActionException {
        FuneralDeadData funeralDeadData = data.getFuneralDead();
        FamilyMemberData familyMemberData = data.getFamilyMember();
        FamilyAgentData familyAgentData = data.getFamilyAgent();

        // 验证默认数据及证件号
        checkFuneralCremationArchives(funeralDeadData, familyMemberData, familyAgentData, 0);

        // 创建逝者信息实体
        FuneralDeadEntity funeralDeadEntity = getEntity(userItem, FuneralDeadEntity.class);
        // 设置业务编码
        funeralDeadData.setBusinessCode(businessCode);
        // 覆盖逝者数据
        BeanUtil.copy2Bean(funeralDeadData, funeralDeadEntity);

        // 插入逝者信息
        funeralDeadMapper.insertFuneralDead(funeralDeadEntity);
        // 判断保存逝者信息结果
        if (DataUtil.invalid(funeralDeadEntity.getId())) {
            throw exception("保存逝者信息失败");
        }

        FuneralBusinessLogEntity businessLogEntity = operationWrapper.businessLog(userItem, businessCode, businessLogCode.getCremationArchivesInsert());

        StringBuilderExt builder = new StringBuilderExt();
        builder.format("业务编码[{0}]，添加逝者，逝者姓名[{1}]", businessCode, funeralDeadEntity.getDeadName());
        // 添加日志
        operationWrapper.operationLog(userItem, businessLogEntity, operationLogCode.getFuneralDeadInsert(), builder);

        // 创建家属信息实体
        FamilyMemberEntity familyMemberEntity = getEntity(userItem, FamilyMemberEntity.class);
        // 设置业务编码
        familyMemberData.setBusinessCode(businessCode);
        // 覆盖请求数据
        BeanUtil.copy2Bean(familyMemberData, familyMemberEntity);
        // 插入家属信息
        familyMemberMapper.insertFamilyMember(familyMemberEntity);
        // 判断保存家属信息结果
        if (DataUtil.invalid(familyMemberEntity.getId())) {
            throw exception("保存家属信息失败");
        }

        builder.clear();
        builder.format("业务编码[{0}]，添加家属信息，家属姓名[{1}]", businessCode, familyMemberEntity.getMemberName());
        // 添加日志
        operationWrapper.operationLog(userItem, businessLogEntity, operationLogCode.getFamilyMemberInsert(), builder);

        // 如果请求存在承办人信息
        if (DataUtil.noNullOrEmpty(familyAgentData)) {
            // 创建承办人信息实体
            FamilyAgentEntity familyAgentEntity = getEntity(userItem, FamilyAgentEntity.class);
            // 设置业务编码
            familyAgentData.setBusinessCode(businessCode);
            // 覆盖承办人数据
            BeanUtil.copy2Bean(familyAgentData, familyAgentEntity);
            // 更新承办人信息
            familyAgentMapper.insertFamilyAgent(familyAgentEntity);
            // 判断保存承办人信息结果
            if (DataUtil.invalid(familyMemberEntity.getId())) {
                throw exception("保存承办人信息失败");
            }

            builder.clear();
            builder.format("业务编码[{0}]，添加承办人信息，承办人单位名称[{1}]，承办人姓名[{2}]", businessCode, familyAgentEntity.getAgentUnitName(), familyAgentEntity.getAgentName());
            // 添加日志
            operationWrapper.operationLog(userItem, businessLogEntity, operationLogCode.getFamilyAgentInsert(), builder);
        }

        // 创建殡葬业务信息实体
        FuneralBusinessEntity funeralBusinessEntity = getEntity(userItem, FuneralBusinessEntity.class);
        // 设置业务编码，更新逝者姓名和家属姓名
        funeralBusinessEntity.setBusinessCode(businessCode)
                .setDeadName(funeralDeadData.getDeadName())
                .setMemberName(familyMemberData.getMemberName());
        funeralBusinessMapper.insertFuneralBusiness(funeralBusinessEntity);
        // 判断保存殡葬业务信息结果
        if (DataUtil.invalid(funeralBusinessEntity.getId())) {
            throw exception("保存殡葬业务信息失败");
        }
    }

    /**
     * 验证证件号码的正确性
     *
     * @param funeralDeadData  逝者数据
     * @param familyMemberData 家属数据
     * @param familyAgentData  承办人数据
     * @param deadId           逝者主键
     * @return
     * @author LiCongLu
     * @date 2020-03-05 14:30
     */
    private void checkFuneralCremationArchives(FuneralDeadData funeralDeadData, FamilyMemberData familyMemberData, FamilyAgentData familyAgentData, Integer deadId) throws ActionException {
        // 验证是否存在逝者及家属信息
        if (DataUtil.isNull(funeralDeadData, familyMemberData)) {
            throw exception("逝者与家属数据不为空");
        }

        // 处理逝者信息
        if (DataUtil.noNullOrEmpty(funeralDeadData)) {

            // 验证是否本地户口
            if (DataUtil.isNull(funeralDeadData.getAsLocal())) {
                funeralDeadData.setAsLocal(0);
            }

            // 验证是否到馆
            if (DataUtil.isNull(funeralDeadData.getAsArrived())) {
                funeralDeadData.setAsArrived(0);
            }

            // 如果姓名同音为空，则赋值姓名
            if (DataUtil.invalid(funeralDeadData.getDeadNameUnison())) {
                funeralDeadData.setDeadNameUnison(funeralDeadData.getDeadName());
            }
            // 生成姓名同音拼音简写
            String deadAbbr = Pinyin4jUtil.getPinyinAbbrToLowerCase(funeralDeadData.getDeadNameUnison());
            funeralDeadData.setDeadNameAbbr(deadAbbr);
        }

        // 处理家属信息
        if (DataUtil.noNullOrEmpty(familyMemberData)) {
            // 如果姓名同音为空，则赋值姓名
            if (DataUtil.invalid(familyMemberData.getMemberNameUnison())) {
                familyMemberData.setMemberNameUnison(familyMemberData.getMemberName());
            }
            // 生成姓名同音拼音简写
            String memberAbbr = Pinyin4jUtil.getPinyinAbbrToLowerCase(familyMemberData.getMemberNameUnison());
            familyMemberData.setMemberNameAbbr(memberAbbr);
        }

        // 验证逝者证件号码
        if (DataUtil.valid(funeralDeadData.getCertificateNo())) {
            // 证件号码存在时，判断证件号类型
            if (DataUtil.isNull(funeralDeadData.getCertificateTypeCode())) {
                throw exception("逝者证件号码不为空时，证件类型编码不能为空");
            }

            // 内地居民身份证
            if (DataUtil.equals(funeralDeadData.getCertificateTypeCode(), dictionaryCode.getCertificateTypeNeiDi())) {
                if (!IdCardUtil.validateCard(funeralDeadData.getCertificateNo())) {
                    throw exception("逝者证件号码非标准内地居民身份证");
                }
            }

            // 验证火化类型(遗体类型)是遗体时验证证件号码是否重复
            if (DataUtil.equals(funeralDeadData.getCremationTypeCode(), dictionaryCode.getCremationTypeYiTi())) {
                String businessCode = funeralDeadDao.hasBusinessCodeForCertificateNo(funeralDeadData.getCertificateNo(), dictionaryCode.getCremationTypeYiTi(), deadId);
                if (DataUtil.valid(businessCode)) {
                    throw exception("火化类型同为遗体的存在重复证件号码");
                }
                // 与家属证号码比较
                if (DataUtil.valid(familyMemberData.getCertificateNo())) {
                    if (DataUtil.equals(funeralDeadData.getCertificateNo(), familyMemberData.getCertificateNo())) {
                        throw exception("逝者证件号码不能与家属证件号码相等");
                    }
                }

                // 与承办人证件号码比较
                if (DataUtil.noNullOrEmpty(familyAgentData)
                        && DataUtil.valid(familyAgentData.getCertificateNo())) {
                    if (DataUtil.equals(funeralDeadData.getCertificateNo(), familyAgentData.getCertificateNo())) {
                        throw exception("逝者证件号码不能与承办人证件号码相等");
                    }
                }
            }


        }

        // 验证家属证件号码
        if (DataUtil.valid(familyMemberData.getCertificateNo())) {
            // 证件号码存在时，判断证件号类型
            if (DataUtil.isNull(familyMemberData.getCertificateTypeCode())) {
                throw exception("家属证件号码不为空时，证件类型编码不能为空");
            }

            // 内地居民身份证
            if (DataUtil.equals(familyMemberData.getCertificateTypeCode(), dictionaryCode.getCertificateTypeNeiDi())) {
                if (!IdCardUtil.validateCard(familyMemberData.getCertificateNo())) {
                    throw exception("家属证件号码非标准内地居民身份证");
                }
            }
        }

        // 验证承办人证件号码
        if (DataUtil.noNullOrEmpty(familyAgentData)
                && DataUtil.valid(familyAgentData.getCertificateNo())) {
            // 证件号码存在时，判断证件号类型
            if (DataUtil.isNull(familyAgentData.getCertificateTypeCode())) {
                throw exception("承办人证件号码不为空时，证件类型编码不能为空");
            }

            // 内地居民身份证
            if (DataUtil.equals(familyAgentData.getCertificateTypeCode(), dictionaryCode.getCertificateTypeNeiDi())) {
                if (!IdCardUtil.validateCard(familyAgentData.getCertificateNo())) {
                    throw exception("承办人证件号码非标准内地居民身份证");
                }
            }
        }
    }

    /**
     * 更新殡葬信息
     *
     * @param userItem 当前帐号
     * @param data     逝者档案数据
     * @return
     * @author LiCongLu
     * @date 2020-02-06 16:43
     */
    private void updateCremationArchives(UserItem userItem, CremationArchivesData data) throws ActionException {
        FuneralDeadData funeralDeadData = data.getFuneralDead();
        FamilyMemberData familyMemberData = data.getFamilyMember();
        FamilyAgentData familyAgentData = data.getFamilyAgent();
        String businessCode = funeralDeadData.getBusinessCode();

        if (DataUtil.isNull(funeralDeadData.getId(), funeralDeadData.getVersion())) {
            throw exception("请求逝者信息主键与版本号不能为空");
        }

        if (DataUtil.isNull(familyMemberData.getId(), familyMemberData.getVersion())) {
            throw exception("请求家属信息主键与版本号不能为空");
        }

        // 通过业务编码获取殡葬信息
        FuneralBusinessViewItem funeralItem = funeralBusinessDao.getFuneralBusinessViewByBusinessCode(businessCode);
        if (DataUtil.isNull(funeralItem) || DataUtil.isNull(funeralItem.getId(), funeralItem.getVersion())) {
            throw exception(MessageConst.NO_FUNERAL_CODE);
        }

        // 验证逝者信息
        if (DataUtil.isNull(funeralItem.getDeadId(), funeralItem.getDeadVersion())
                || !DataUtil.equals(funeralItem.getDeadId(), funeralDeadData.getId())) {
            throw exception("业务编码错误，不存在此殡葬逝者信息");
        }

        // 验证逝者主键
        if (!DataUtil.equals(funeralItem.getDeadId(), funeralDeadData.getId())) {
            throw exception("逝者主键错误，与按业务编码获取主键不匹配");
        }

        // 验证家属信息
        if (DataUtil.isNull(funeralItem.getMemberId(), funeralItem.getMemberVersion())
                || !DataUtil.equals(funeralItem.getMemberId(), familyAgentData.getId())) {
            throw exception("业务编码错误，不存在此殡葬家属信息");
        }

        // 验证家属主键
        if (!DataUtil.equals(funeralItem.getMemberId(), familyMemberData.getId())) {
            throw exception("家属主键错误，与按业务编码获取主键不匹配");
        }

        // 验证证件号码
        checkFuneralCremationArchives(funeralDeadData, familyMemberData, familyAgentData, funeralItem.getDeadId());

        // 获取最新逝者信息
        FuneralDeadEntity funeralDeadEntity = funeralDeadMapper.getFuneralDeadById(funeralItem.getDeadId());
        // 验证逝者信息版本
        if (!DataUtil.equals(funeralDeadData.getVersion(), funeralDeadEntity.getVersion())) {
            throw exception(versionError("逝者信息"));
        }

        // 验证业务编码
        if (!DataUtil.equals(funeralDeadData.getBusinessCode(), funeralDeadEntity.getBusinessCode())) {
            throw exception(MessageConst.BUSINESS_CODE_ERROR);
        }

        // 覆盖逝者数据
        BeanUtil.copy2Bean(funeralDeadData, funeralDeadEntity);
        // 设置修改帐号
        funeralDeadEntity.setBusinessCode(businessCode)
                .setModifiedUserId(userItem.getId())
                .setId(funeralItem.getDeadId());
        // 更新逝者信息
        funeralDeadMapper.updateFuneralDead(funeralDeadEntity);

        FuneralBusinessLogEntity businessLogEntity = operationWrapper.businessLog(userItem, businessCode, businessLogCode.getCremationArchivesUpdate());

        StringBuilderExt builder = new StringBuilderExt();
        builder.format("业务编码[{0}]，更新逝者，逝者姓名[{1}]", businessCode, funeralDeadEntity.getDeadName());
        // 添加日志
        operationWrapper.operationLog(userItem, businessLogEntity, operationLogCode.getFuneralDeadUpdate(), builder);

        // 获取家属信息
        FamilyMemberEntity familyMemberEntity = familyMemberMapper.getFamilyMemberById(funeralItem.getAgentId());
        // 验证家属信息版本
        if (familyMemberData.getVersion().intValue() != familyMemberEntity.getVersion().intValue()) {
            throw exception(versionError("家属信息"));
        }

        // 验证业务编码
        if (!DataUtil.equals(familyMemberData.getBusinessCode(), familyMemberEntity.getBusinessCode())) {
            throw exception(MessageConst.BUSINESS_CODE_ERROR);
        }

        // 覆盖请求数据
        BeanUtil.copy2Bean(familyMemberData, familyMemberEntity);
        // 设置修改帐号
        familyMemberEntity.setBusinessCode(businessCode)
                .setModifiedUserId(userItem.getId());
        // 更新家属信息
        familyMemberMapper.updateFamilyMember(familyMemberEntity);

        builder.clear();
        builder.format("业务编码[{0}]，更新家属信息，家属姓名[{1}]", businessCode, familyMemberEntity.getMemberName());
        // 添加日志
        operationWrapper.operationLog(userItem, businessLogEntity, operationLogCode.getFamilyMemberUpdate(), builder);

        // 获取殡葬业务信息
        FuneralBusinessEntity funeralBusinessEntity = funeralBusinessMapper.getFuneralBusinessById(funeralItem.getId());
        // 更新逝者姓名和家属姓名
        funeralBusinessEntity.setDeadName(funeralDeadData.getDeadName())
                .setMemberName(familyMemberData.getMemberName())
                .setModifiedUserId(userItem.getId());
        funeralBusinessMapper.updateFuneralBusiness(funeralBusinessEntity);

        // 更新火化档案承办人信息
        updateCremationArchivesAgent(userItem, familyAgentData, businessCode, funeralItem, businessLogEntity);
    }

    /**
     * 更新火化档案承办人信息
     *
     * @param userItem          当前帐号
     * @param familyAgentData   承办人数据
     * @param businessCode      业务编码
     * @param businessLogEntity 业务日志
     * @param viewItem          火化档案数据
     * @return
     * @author LiCongLu
     * @date 2020-02-13 15:17
     */
    private void updateCremationArchivesAgent(UserItem userItem, FamilyAgentData familyAgentData, String businessCode, FuneralBusinessViewItem viewItem, FuneralBusinessLogEntity businessLogEntity) throws ActionException {
        // 如果请求存在承办人信息
        if (DataUtil.noNullOrEmpty(familyAgentData)) {

            if (DataUtil.isNull(familyAgentData.getId(), familyAgentData.getVersion())) {
                throw exception("请求承办人信息主键与版本号不能为空");
            }

            // 验证承办人信息
            if (DataUtil.isNull(viewItem.getAgentId(), viewItem.getAgentId())) {
                throw exception("业务编码错误，不存在此殡葬承办人信息");
            }

            // 验证承办人主键
            if (viewItem.getAgentId().intValue() != familyAgentData.getId().intValue()) {
                throw exception("承办人主键错误，与按业务编码获取主键不匹配");
            }

            // 获取承办人信息
            FamilyAgentEntity familyAgentEntity = familyAgentMapper.getFamilyAgentById(viewItem.getAgentId());
            // 验证承办人信息版本
            if (familyAgentData.getVersion().intValue() != familyAgentEntity.getVersion().intValue()) {
                throw exception(versionError("承办人信息"));
            }

            // 验证业务编码
            if (!familyAgentData.getBusinessCode().equals(familyAgentEntity.getBusinessCode())) {
                throw exception(MessageConst.BUSINESS_CODE_ERROR);
            }

            // 覆盖承办人数据
            BeanUtil.copy2Bean(familyAgentData, familyAgentEntity);
            // 设置修改帐号
            familyAgentEntity.setBusinessCode(businessCode)
                    .setModifiedUserId(userItem.getId());
            // 更新承办人信息
            familyAgentMapper.updateFamilyAgent(familyAgentEntity);

            StringBuilderExt builder = new StringBuilderExt();
            builder.format("业务编码[{0}]，更新承办人信息，承办人单位名称[{1}]，承办人姓名[{2}]", businessCode, familyAgentEntity.getAgentUnitName(), familyAgentEntity.getAgentName());
            // 添加日志
            operationWrapper.operationLog(userItem, businessLogEntity, operationLogCode.getFamilyAgentUpdate(), builder);
        }
    }

    /**
     * 根据funeral_dead表中businessCode加载打印逝者二维码
     *
     * @param data 业务编码数据
     * @return
     * @author GaoXiangLiang
     * @date 2020-06-30 13:45
     */
    public BaseResult<CremationArchivesItem> loadPrintCremationArchivesCodeWithBusinessCode(BusinessCodeData data) {
        String businessCode = data.getBusinessCode();
        // 通过业务编码获取殡葬信息
        FuneralBusinessViewItem funeralItem = funeralBusinessDao.getFuneralBusinessViewByBusinessCode(businessCode);
        if (DataUtil.isNull(funeralItem) || DataUtil.isNull(funeralItem.getId(), funeralItem.getVersion())) {
            throw exception(MessageConst.NO_FUNERAL_CODE);
        }

        // 创建响应实体
        CremationArchivesItem loadItem = new CremationArchivesItem();

        // 获取逝者信息
        loadItem.setFuneralDead(funeralDeadDao.getFuneralDeadByBusinessCode(businessCode));
        // 设置字典文本
        loadItem.getFuneralDead().setDeadGender(dictionaryWrapper.getDataText(EDictCode.SEX, loadItem.getFuneralDead().getDeadGenderCode()));
        // 获取家属信息
        loadItem.setFamilyMember(familyMemberDao.getFamilyMemberByBusinessCode(businessCode));
        // 获取承办人信息
        loadItem.setFamilyAgent(familyAgentDao.getFamilyAgentByBusinessCode(businessCode));

        // 返回响应结果
        return successData(loadItem);
    }
    /**
     * 根据funeral_dead表中businessCode加载打印逝者二维码新接口
     * @param data 业务编码数据
     * @return
     * @author GaoXiangLiang
     * @date 2020-08-25 13:45
     */
    public BaseResult<CremationPrintInfo> loadPrintCremationArchivesWithBusinessCode(BusinessCodeData data) {
        String businessCode = data.getBusinessCode();
        // 通过业务编码获取殡葬信息
        FuneralBusinessViewItem funeralItem = funeralBusinessDao.getFuneralBusinessViewByBusinessCode(businessCode);
        if (DataUtil.isNull(funeralItem) || DataUtil.isNull(funeralItem.getId(), funeralItem.getVersion())) {
            throw exception(MessageConst.NO_FUNERAL_CODE);
        }

        // 创建响应实体
        CremationPrintInfo loadItem = new CremationPrintInfo();
        //拷贝数据
         BeanUtil.copy2Bean(funeralItem,loadItem);
        // 设置字典文本
        loadItem.setDeadGender(dictionaryWrapper.getDataText(EDictCode.SEX, loadItem.getDeadGenderCode()));
        // 获取家属信息
        FamilyMemberItem familyMemberItem=familyMemberDao.getFamilyMemberByBusinessCode(businessCode);
        loadItem.setMemberName(null!=familyMemberItem?familyMemberItem.getMemberName():"");
        // 返回响应结果
        return successData(loadItem);
    }
}
